package net.wofly.cashier.order.validator;

import net.wofly.cashier.order.domain.Order;
import net.wofly.cashier.order.domain.OrderContent;
import net.wofly.cashier.order.util.OrderType;
import net.wofly.cashier.order.util.PayType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

public class OrderValidator {
    
    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");
    
    public static String validatePlaceOrderParam(Order order) {
        StringBuffer stringBuffer = new StringBuffer();
        if (null == order) {
            stringBuffer.append("订单实体不能为空").append(lineSeparator);
        } else {
            if (null == order.getOrderType() || order.getOrderType().intValue() < 0 || !OrderType.isValueCorrect(
                    order.getOrderType())) {
                stringBuffer.append("订单类型不能为空，或者订单类型不正确").append(lineSeparator);
            }
            if (StringUtils.isEmpty(order.getTableID())) {
                stringBuffer.append("订单所使用的餐桌ID不能为空").append(lineSeparator);
            }
            
            if (CollectionUtils.isEmpty(order.getOrderContents())) {
                stringBuffer.append("点菜内容（菜品清单）不能为空").append(lineSeparator);
            } else {
                validateOrderContent(order.getOrderContents(), stringBuffer);
            }
            
        }
        return stringBuffer.toString();
    }
    
    public static String validateAddDishedParam(String orderID, List<OrderContent> orderContents) {
        StringBuffer stringBuffer = new StringBuffer();
            if (StringUtils.isEmpty(orderID)) {
                stringBuffer.append("订单ID不能为空").append(lineSeparator);
            }
            
            if (CollectionUtils.isEmpty(orderContents)) {
                stringBuffer.append("加菜的菜品不能为空").append(lineSeparator);
            } else {
                validateOrderContent(orderContents, stringBuffer);
            }
            
        return stringBuffer.toString();
    }

    private static void validateOrderContent(List<OrderContent> orderContents, StringBuffer stringBuffer) {
        for (OrderContent content : orderContents) {
            if (content == null) {
                stringBuffer.append("点菜内容（单个菜品实体）不能为空").append(lineSeparator);
            } else {
                /*if (content.getBatch() == null || (content.getBatch().intValue() != 1 && content.getBatch()
                        != 2)) {
                    stringBuffer.append("点菜批次不能为空，或者批次值不正确").append(lineSeparator);
                }*/
                if (content.getCommodityID() == null) {
                    stringBuffer.append("菜品ID不能为空").append(lineSeparator);
                }
                if (content.getNum() == null || content.getNum() < 1) {
                    stringBuffer.append("菜品数量不能为空，并且数量不能少于1").append(lineSeparator);
                }
            }
        }
    }

    public static String validatePayParams(String orderID, Integer payType, Integer sum, String payAccount){
        StringBuffer stringBuffer = new StringBuffer();
        
        if(StringUtils.isEmpty(orderID)){
            stringBuffer.append("订单ID不能为空").append(lineSeparator);
        }
        if(null == payType || !PayType.isValueCorrect(payType)){
            stringBuffer.append("支付类型不能为空，并且").append(lineSeparator);
        }
        if(StringUtils.isEmpty(orderID)){
            stringBuffer.append("订单ID不能为空").append(lineSeparator);
        }
        if(StringUtils.isEmpty(orderID)){
            stringBuffer.append("订单ID不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateTableID(String tableID){
        StringBuffer stringBuffer = new StringBuffer();
        
        if(StringUtils.isEmpty(tableID)){
            stringBuffer.append("餐桌ID不能为空").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
}
